package com.lanou.musicbox.fragment.localmusic;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.localmusic.LocalityAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.FindSongs;
import com.lanou.musicbox.custom.Events;
import com.lanou.musicbox.entity.Entity;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by 柳柏庆 on 15/10/20.
 * 本地音乐列表页面
 */
public class LocalityFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    private ListView locality_listview;
    private View view; //设置头布局
    private LocalityAdapter adapter;
    private Button Finder;
    private List<Entity> mp3Infos;
    private FindSongs findSongs;
    private PassLocalMusic listener;
    private TextView songs_count;

    /**
     * 实现接口
     *
     * @return
     */
    public LocalityAdapter getAdapter() {
        return adapter;
    }

    public interface PassLocalMusic {

        void onItemWhere(int position);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        listener = (PassLocalMusic) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.locality_listview, null);
        locality_listview = (ListView) view.findViewById(R.id.locality_listview);
        Finder = (Button) view.findViewById(R.id.Finder);
        songs_count = (TextView) view.findViewById(R.id.song_count);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        locality_listview.setCacheColorHint(1); // 去除listView拖动背景颜色
        locality_listview.setOnItemClickListener(this);

        findSongs = new FindSongs();
        mp3Infos = findSongs.getMp3Infos(getActivity().getContentResolver());

        adapter = new LocalityAdapter(getActivity().getApplicationContext(), mp3Infos);
        findSongs.setListAdpter(getActivity().getApplicationContext(), mp3Infos, locality_listview);
        songs_count.setText(mp3Infos.size() + "");


        Finder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp3Infos = findSongs.getMp3Infos(getActivity().getContentResolver());
                Events.MusicInfoEvent musicInfoEvent = new Events.MusicInfoEvent();
                String whatMusic = "本地";
                musicInfoEvent.setMp3Infos(mp3Infos, whatMusic);
                EventBus.getDefault().post(musicInfoEvent);

            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Events.MusicInfoEvent musicInfoEvent = new Events.MusicInfoEvent();
        String whatMusic = "本地";
        musicInfoEvent.setMp3Infos(mp3Infos, whatMusic);
        EventBus.getDefault().post(musicInfoEvent);
        listener.onItemWhere(position);

    }

}